import { App, DynamicEnvironment, Environment, RosourceItemsReturnType, getEnvmapFromHDRTexture } from "vjmap3d";
import { LinearSRGBColorSpace, SRGBColorSpace, RepeatWrapping, Vector3, NearestFilter, LinearMipMapLinearFilter, UnsignedByteType, Object3D } from "three";


export const initResConfig = (allResources: RosourceItemsReturnType) => {
    
    let ut_startroom_ao_texture = allResources.getTexture("ut_startroom_ao")
    ut_startroom_ao_texture.flipY = false;
    ut_startroom_ao_texture.colorSpace = LinearSRGBColorSpace;
    ut_startroom_ao_texture.channel = 1;

    let ut_startroom_light_texture = allResources.getTexture("ut_startroom_light")
    ut_startroom_light_texture.flipY = false;
    ut_startroom_light_texture.colorSpace = SRGBColorSpace;
    ut_startroom_light_texture.channel = 1;

    let ut_floor_normal_texture = allResources.getTexture("ut_floor_normal")
    ut_floor_normal_texture.flipY = false;
    ut_floor_normal_texture.colorSpace = LinearSRGBColorSpace;
    ut_floor_normal_texture.wrapS = RepeatWrapping;
    ut_floor_normal_texture.wrapT = RepeatWrapping;

    let ut_floor_roughness_texture = allResources.getTexture("ut_floor_roughness");
    ut_floor_roughness_texture.flipY = false;
    ut_floor_roughness_texture.colorSpace =  LinearSRGBColorSpace;
    ut_floor_roughness_texture.wrapS = RepeatWrapping;
    ut_floor_roughness_texture.wrapT = RepeatWrapping;

    let ut_car_body_ao = allResources.getTexture("ut_car_body_ao");
    ut_car_body_ao.flipY = false;
    ut_car_body_ao.colorSpace = LinearSRGBColorSpace;
    ut_car_body_ao.minFilter = NearestFilter;
    ut_car_body_ao.magFilter = NearestFilter;
    ut_car_body_ao.channel = 1;
}


export const initCameraState = async (app: App) => {
    app.cameraControl.loadStateAsync({
        cameraTarget: new Vector3(-0.5069180490909221,1.044247322065262, -0.8414593880297994),
        cameraPosition: new Vector3(-0.5069180490909214,1.4479590731258034, -6.392495965112243)
    }, 3000)
}

export const setRunCameraState = async (app: App) => {
    app.cameraControl.loadStateAsync({
        cameraTarget: new Vector3(-1.762289609856335,1.0486569596845907, -0.516193341154058),
        cameraPosition: new Vector3(-4.892778717904699,2.9751708079496586, -3.4517726557828343),
    }, 1000);
    return app.cameraControl.shake(100000, 10, 0.02, true)
}

export const initEnvironment = (app: App, allResources: RosourceItemsReturnType) => {
    let nightHdr = allResources.getResource("ut_env_night");
    let lightHdr = allResources.getResource("ut_env_light");
    const envmap1 = getEnvmapFromHDRTexture(
        app.renderer,
        nightHdr,
      );
    const envmap2 = getEnvmapFromHDRTexture(
        app.renderer,
        lightHdr,
    );
    app.scene.environment = envmap2;
    let env = new DynamicEnvironment(app, {
        envmap1: envmap1,
        envmap2: envmap2
    })
    app.scene.environment = env.texture;
    env.setWeight(0);
    env.setIntensity(0)
    env.lerpWeight(1, 3000);
    env.lerpIntensity(1, 3000);
    return env;
}

export const createRunEnviroment = (app: App, ignoreObjects: Object3D[]) => {
    return new Environment(app, {
        resolution: 512,
        scene: app.scene,
        renderOptions: {
          minFilter: LinearMipMapLinearFilter,
          anisotropy: 0,
          depthBuffer: false,
          generateMipmaps: true,
        },
        textureType: UnsignedByteType,
        ignoreObjects: ignoreObjects
    });
}